//
//  ZTodoMissionDetailVC.m
//  WeClubs
//
//  Created by Zed Link on 10/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMissionDetailVC.h"
#import "ZTodoMissionView.h"
#import "ZCommentCell.h"
#import "ZCommentHeader.h"

static NSString * const MissionCommentCellReuseID = @"ZMissionCommentCellReuseID";
static NSString * const CommentHeaderCellReuseID = @"ZCommentHeaderCellReuseID";

@interface ZTodoMissionDetailVC () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet ZTodoMissionView *missionHeaderView;
@property (weak, nonatomic) IBOutlet UITableView *mainView;

/** 所有评论 */
@property (strong, nonatomic) NSArray *commentModels;

@end

@implementation ZTodoMissionDetailVC

+ (instancetype)zNew
{
    return [self zStoryboardInstance];
}

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"任务详情";
    
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
}

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.mainView.rowHeight = UITableViewAutomaticDimension;
    self.mainView.estimatedRowHeight = 60;
    self.mainView.sectionHeaderHeight = 36;
    self.mainView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.mainView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:CommentHeaderCellReuseID];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.missionHeaderView.model = self.missionModel;
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"source_type" : @"mission",
                               @"source_id" : @(self.missionModel.missionId),
                               };

    [HTTPRequest POST:kGet_comment_listUrl parameter:dataPara success:^(id resposeObject) {
        // Todo: 加载下一页数据
        NSArray *comments =resposeObject[@"data"][@"comment"];
        NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
        for (NSDictionary *dict in comments) {
            ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
            [tempArrM addObject:model];
        }
        self.commentModels = tempArrM;
        
    } failure:^(NSError *error) {
       
    }];
//    [[ZNetManager sharedInstance] updateCommentsOfTodoItem:1 withItemId:self.missionModel.missionId success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//     {
//         // Todo: 加载下一页数据
//         NSArray *comments = data[@"comment"];
//         
//         NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
//         for (NSDictionary *dict in comments) {
//             ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
//             [tempArrM addObject:model];
//         }
//         self.commentModels = tempArrM;
//         
//     } failure:nil];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCommentModel *commentModel = self.commentModels[indexPath.row];
    
    ZCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:MissionCommentCellReuseID];
    cell.commentModel = commentModel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 36;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CommentHeaderCellReuseID];
    header.style = ZTableHeaderStyleComment;
    header.alignment = ZTableHeaderAlignmentCenter;
    
    NSString *count = [NSString stringWithFormat:@"共%zd条回复", self.commentModels.count];
    NSDictionary *attr = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightLight],
                           NSForegroundColorAttributeName : [UIColor zColorWithHex:0x666666],
                           };
    [header.descriptionBtn setAttributedTitle:[[NSAttributedString alloc] initWithString:count attributes:attr] forState:UIControlStateNormal];
    [header.descriptionBtn setImage:[UIImage imageNamed:@"ic_comment"] forState:UIControlStateNormal];
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 12;
}

#pragma mark - Getter & Setter

- (void)setCommentModels:(NSArray *)commentModels
{
    _commentModels = commentModels;
    [self.mainView reloadData];
}

@end
